<?php
namespace App\Think\Org;

use App\Think\Db;

/**
 * 邮箱验证码类
 */
class Ems
{
    /**
     * 验证码有效时长
     * @var int
     */
    protected static $expire = 120;

    /**
     * 最大允许检测的次数
     * @var int
     */
    protected static $maxCheckNums = 10;

    /**
     * 获取最后一次邮箱发送的数据
     *
     * @param   int    $email 邮箱
     * @param   string $event 事件
     * @return  Ems
     */
    public static function get($email, $event = 'default')
    {
        $ems = Db::name('ems')->where("email='{$email}' AND event='{$event}'")->order('id DESC')->find();
        // Hook::listen('ems_get', $ems, null, true);
        return $ems ? $ems : null;
    }

    /**
     * 发送验证码
     *
     * @param   int    $email 邮箱
     * @param   int    $code  验证码,为空时将自动生成4位数字
     * @param   string $event 事件
     * @return  boolean
     */
    public static function send($email, $code = null, $event = 'default', $ip = '')
    {
        $code = is_null($code) ? mt_rand(1000, 9999) : $code;
        $time = time();
        $_insert_param = ['event' => $event, 'email' => $email, 'code' => $code, 'ip' => $ip, 'createtime' => $time];
        $_insert_id = Db::name('ems')->insert($_insert_param);
        // $result = Hook::listen('ems_send', $ems, null, true);
        $result = \App\Think\Sdk\Ems::send($_insert_param);
        if (!$result) {
            Db::name('ems')->where("id='{$_insert_id}'")->delete();
            return false;
        }
        return true;
    }

    /**
     * 发送通知
     *
     * @param   mixed  $email    邮箱,多个以,分隔
     * @param   string $msg      消息内容
     * @param   string $template 消息模板
     * @return  boolean
     */
    public static function notice($email, $msg = '', $template = null)
    {
        $params = [
            'email'    => $email,
            'msg'      => $msg,
            'template' => $template
        ];
        // $result = Hook::listen('ems_notice', $params, null, true);
        return $result ? true : false;
    }

    /**
     * 校验验证码
     *
     * @param   int    $email 邮箱
     * @param   int    $code  验证码
     * @param   string $event 事件
     * @return  boolean
     */
    public static function check($email, $code, $event = 'default')
    {
        $time = time() - self::$expire;
        $ems = Db::name('ems')::where("email='{$email}' AND event='{$event}'")->order('id DESC')->find();
        if ($ems) {
            if ($ems['createtime'] > $time && $ems['times'] <= self::$maxCheckNums) {
                $correct = $code == $ems['code'];
                if (!$correct) {
                    Db::name('ems')->where("id='{$ems['id']}'")->update(['times'=>Db::raw('times+1')]);
                    return false;
                } else {
                    // $result = Hook::listen('ems_check', $ems, null, true);
                    return true;
                }
            } else {
                // 过期则清空该邮箱验证码
                self::flush($email, $event);
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 清空指定邮箱验证码
     *
     * @param   int    $email 邮箱
     * @param   string $event 事件
     * @return  boolean
     */
    public static function flush($email, $event = 'default')
    {
        Db::name('ems')->where("email='{$email}' AND event='{$event}'")->delete();
        // Hook::listen('ems_flush');
        return true;
    }
}
